package com.hongdee.crm.task.dao.impl;

import com.hongdee.base.basedao.impl.BaseDaoImpl;
import com.hongdee.crm.task.dao.ITCrmCarPropertyTaskDao;
import com.hongdee.crm.task.entity.TCrmCarProperty;
import com.hongdee.crm.task.entity.TCrmTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/3/15.
 */
@Service
public class TCrmCarPropertyTaskDaoImpl extends BaseDaoImpl implements ITCrmCarPropertyTaskDao {

    @Autowired
    @Qualifier("jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Map<String, Object>> findCarList(String sql,int start,int limit)throws Exception {
        List<Map<String,Object>> lists=this.getJdbcDao().queryForMapListBySqlWithParams(sql,start,limit);
        if(lists.size()>0){
            return lists;
        }
        return null;
    }

    @Override
    public Integer findPropertyByIdAndLabel(String carId, String label) throws Exception {
        String sql="select * from t_crm_car_property where car_id=? and label=?";

        return this.getJdbcDao().queryForMapListBySqlWithParams(sql,carId,label).size();
    }

    @Override
    public void insertCarPropertyLists(final List<TCrmCarProperty> lists){
        String sql="INSERT INTO t_crm_car_property (`id`, `car_id`, `label`) VALUES (replace(UUID(),'-',''), ?, ?)";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                TCrmCarProperty property=lists.get(i);
                ps.setString(1,property.getCarId());
                ps.setString(2,property.getLabel());
            }

            @Override
            public int getBatchSize() {
                return lists.size();
            }
        });
    }
}
